<template>
<!--  首页面-->
  <div style="margin-left: 5rem">
    <el-text>所有航班信息:</el-text>
    <el-button type="primary" size="small" v-show="this.isAdmin" style="float: right" @click="changeEvent=true">
      新增航班
    </el-button>
    <el-table
        :data="tableData"
        style="width: 45rem"
        height="250">
      <el-table-column
          prop="flightNo"
          label="航班编号"
          width="120">
      </el-table-column>
      <el-table-column
          fixed
          prop="flightInform"
          label="航班信息"
          width="150">
      </el-table-column>
      <el-table-column
          prop="ticketNum"
          label="剩余机票数"
          width="120">
      </el-table-column>
      <el-table-column
          prop="notice"
          label="通知"
          width="140">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="auto">
        <template v-slot="scope">
          <el-button type="primary" size="small" v-show="this.isAdmin" @click="editData(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" v-show="this.isAdmin" @click="deleteData(scope.row.id)">删除</el-button>
          <el-button type="primary" size="small" v-show="!this.isAdmin" @click="buyTicket(scope.row.flightNo)">购买</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
<!--弹出框   v-model为true时显示 :close-on-click-modal-防止失去焦点后关闭     :show-close去除右上角的X-->
  <el-dialog title="操作航班信息" style="width: 30rem;" v-model="this.changeEvent" :close-on-click-modal="false"
             :show-close="false">
    <el-form ref="rowData" :model="this.rowData" label-width="80px" style="margin-left: 3rem">
      <el-form-item label="航班信息">
        <!--v-model双向绑定数据-->
        <!-- 1rem===16px-->
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.flightInform"
        ></el-input>
      </el-form-item>
      <el-form-item label="剩余机票">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.ticketNum"
        ></el-input>
      </el-form-item>
      <el-form-item label="通知">
        <el-input
            style="width: 15rem;"
            v-model="this.rowData.notice"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="this.submitRowData()">提交</el-button>
        <el-button @click="this.closeDialog()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>


</template>
<script>
import request from "@/utils/request.js";

export default {
  name: "Home",
  // 数据
  data() {
    return {
      // 弹出框
      changeEvent: false,
      // 是否为管理员
      isAdmin: true,
      //表格所有数据
      tableData: [],
      // 编辑行数据
      rowData: {},
      // 登录用户信息
      userInformation: {},
      // 购票数据
      userTicket: {},
    }
  },
  // 初始化时调用
  created() {
    // this.getTableData();
    // this.getInformation()
  },
  // 方法
  methods: {
    getTableData() {
      request.get("/ticket/all").then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data;
        }
      })
    },
    //新增或者编辑航班数据
    editData(id) {
      // 获取行数据
      request.get("/ticket/" + id).then(res => {
        if (res.data.code === '200') {
          this.rowData = res.data.data;
        }
      })
      // 显示弹窗
      this.changeEvent = true;
    },
    // 删除航班数据
    deleteData(id) {
      request.get("/ticket/delete/" + id).then(res => {
        if (res.data.code === '200') {
          this.success()
        }
      })
    },
    // 提交行数据
    submitRowData() {
      request.post("/ticket/edit", this.rowData).then(res => {
        if (res.data.code === '200') {
          this.success()
        }
      })
    },
    // 买票
    buyTicket(flightNo) {
      // 查询是否已购买该机票
      request.get("/user_ticket/getOne/?account="+this.userInformation.account+"&flightNo="+flightNo).then(res=>{
        if (res.data.data!=null){
          this.$message.error("已有该机票")
          return;
        }
        this.userTicket.flightNo = flightNo
        this.userTicket.account = this.userInformation.account
        request.post("/user_ticket/buyTicket",this.userTicket).then(res=>{
          if (res.data.code==='200'){
            this.success()
          }
        })
      })
    },
    closeDialog() {
      // 关闭弹窗
      this.changeEvent = false;
      // 清空数据
      this.rowData = {};
    },
    // 从浏览器sessionStorage中获取用户信息
    getInformation() {
      this.userInformation = JSON.parse(sessionStorage.getItem("user"))
      if (this.userInformation.userRole === '20231111') {
        this.isAdmin = true;
      }
    },
    success(){
      //关闭弹窗
      this.changeEvent = false;
      // 重新获取表格数据
      this.getTableData();
      // 清空提交信息
      this.rowData = {};
      // 显示提交成功
      this.$message.success("操作成功")
    }
  }
}
</script>

<style scoped>

</style>